﻿using UnityEngine;

/// <summary>
/// 单例继承这个类，自动在需要的时候创建游戏物体
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
	public Singleton()
	{
		Init();
	}

	private static T current;
	public static T Current
	{
		get
		{
			if (current != null) return current;

			GameObject go = new GameObject();
			current = go.AddComponent<T>();
			DontDestroyOnLoad(go);

			return current;
		}
	}

	/// <summary>
	/// 初始化操作，被构造方法调用
	/// </summary>
	protected abstract void Init();

	/// <summary>
	/// Unity 自动调用
	/// </summary>
	protected virtual void Awake()
	{
		gameObject.name = GetType().Name;
	}
}

